package base

import (
	"jvm/rtda"
	"jvm/rtda/heap"
)

func InitClass(jThread *rtda.JThread, class *heap.Class) {
	class.StartedInit()
	scheduleClinit(jThread, class)
	initSuperClass(jThread, class)
}

func initSuperClass(jThread *rtda.JThread, class *heap.Class) {
	if !class.IsInterface() {
		superClass := class.SuperClass()
		if superClass != nil && !superClass.InitStarted() {
			InitClass(jThread, superClass)
		}
	}
}

func scheduleClinit(jThread *rtda.JThread, class *heap.Class) {
	clinit := class.GetClinitMethod()
	if clinit != nil {
		newJFrame := jThread.NewJFrame(clinit)
		jThread.PushJFrame(newJFrame)
	}
}
